package org.lion.proxy;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by lion on 3/17/17.
 */
public abstract class BaseInput {

    public Map toMap() {
        Map<String, String> map = new HashMap();
        Class<? extends BaseInput> clazz = this.getClass();
        Method[] methods = clazz.getMethods();
        for (Method method : methods) {
            String name = method.getName();
            if (name.equals("getClass")) {
                continue;
            }
            if (name.startsWith("get")) {
                String pname = name.substring(3);
                if ("".equals(pname)) {
                    continue;
                }
                pname = pname.substring(0, 1).toLowerCase() + pname.substring(1);
                Object pvalue = null;
                try {
                    pvalue = method.invoke(this);
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                }
                if (pvalue != null && !"".equals(pvalue)) {
                    map.put(pname, pvalue.toString());
                }
            }
        }
        return map;
    }

}
